package cn.edu.scau.cmi.chenbingquan.multiton;

import java.util.ArrayList;

// 元帅类(10个实例)
public class Marshal {
	public static final int TOTAL_AMOUNT = 10;
	public static ArrayList<Marshal> instances = new ArrayList<>();		// 存放元帅对象
	private String name;
	
	private Marshal(String name) {
		this.name = name;
	}
	
	// 创建元帅
	public static ArrayList<Marshal> getInstances(String name) {
		if(instances.size() < TOTAL_AMOUNT && isNameExisted(name) == false) {
			Marshal temp = new Marshal(name);
			instances.add(temp);
		}
		return instances;
	}
	
	// 判断元帅是否重复
	public static boolean isNameExisted(String name) {
		for(int i=0; i<instances.size(); i++) {
			if(instances.get(i).name.equals(name)) {
				return true;
			}
		}
		return false;
	}
	
	@Override
	public String toString() {
		return "元帅名称为：" + this.name;
	}
}
